package model;

public class BubbleSort extends Sorter{

	public BubbleSort() {
		super();
		statistics = new Statistics();
	}
	@SuppressWarnings("unchecked")
	public void sort(Comparable[] x) {
	    int n = x.length;
	    boolean doMore = true;
	    long beforeTime = System.nanoTime();
	    
	    while (doMore) {
	        n--;
	        doMore = false;  // assume this is our last pass over the array
	        for (int i=0; i<n; i++) {
	            	//Check
	        	statistics.incrementChecks();
	            if (x[i].compareTo( x[i+1] ) > 0) {
	        	//Swap
	        	statistics.incrementSwaps();
	                // exchange elements
	                Integer temp =  (Integer) x[i];  
	                x[i] = x[i+1];  
	                x[i+1] = temp;
	                //-----------------
	                if(simulation)
	                PushSwapToQueue(i, i+1);
	                //-----------------
	                doMore = true;  // after an exchange, must look again 
	            }
	        }
	    }
	    long afterTime = System.nanoTime();
	    statistics.setRunTime(afterTime - beforeTime);
	}
	
	@Override
	public int getComplexity(int arraySize) {
	    return (int) Math.pow(arraySize, 2);
	}


}
